package org.dei.perla.test.llqruntime.simulator;

import java.util.Random;
import org.dei.perla.parser.expressions.ConstantFloat;
import org.dei.perla.sys.query.llqruntime.datastructures.*;


/**
 * Riproduce un fintissimo device, che sputa un record a comando.
 * NON RAPPRESENTA IL CASO REALE!
 * @author diego
 *
 */
public class DeviceMockup {
	
	
	private QueryRecordStructure pStructure = new QueryRecordStructure();
	private Random tRandom = new Random();	
		
	public boolean addCapability(String parAttributeName, Class<?> parAttributeClass) {
		try {		
			pStructure.addFieldStructure(parAttributeName, Class.forName(parAttributeClass.getName()));
			return true;
		} catch (ClassNotFoundException e) {
			System.out.println("Class "+parAttributeClass.getCanonicalName()+" not found!");
			return false;
		}
	}
	
	//spilla un record dal device, a seconda di come e' stato costruito con addCapability
	public synchronized Record generateRecord() {
		
		//0<=x<=11
		Record tRecord = new Record(pStructure);
		for (String t : pStructure.getRecordStructure().keySet()) {
			tRecord.setField(t, new ConstantFloat(tRandom.nextFloat()+tRandom.nextInt(10)));
		}
		return tRecord;
	}
	
	public QueryRecordStructure getSamplableAttributes() {
		return this.pStructure;
	}
	
}